package org.oschina.api.manage.config.dao;

import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.manage.config.model.Config;
import org.oschina.api.manage.config.vo.ConfigVo;
import org.springframework.stereotype.Repository;

@Repository
public class ConfigDAO extends BaseDAO {

	private final String findVo[]={"id","name","value"};
	
	/**
	 * 返回
	 * @return
	 */
	public List<ConfigVo> findVo()
	{
		return getQueryHelper().query_cache(findVo, null,new String[]{"ORDER BY id"}, null, ConfigVo.class, -1,-1);
	}
	
	/**
	 * 更新
	 * @param config
	 * @return
	 */
	public int update(Config config)
	{
		return (int) getQueryHelper().update_cache(new String[]{"name=?","value=?"}, 
				new String[]{"id=?"}, new Object[]{config.getName(),config.getValue(),config.getId()}, ConfigVo.class);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public ConfigVo findVoById(int id)
	{
		ConfigVo configVo=(ConfigVo) getCacheHelper().hget(getKey(Constants.CACHE_CONFIG,id),ConfigVo.class);
		if(configVo!=null)
			return configVo;
		
		return _findById(id);
	}
	private ConfigVo _findById(int id)
	{
		return (ConfigVo) getQueryHelper().queryForObject_cache(findVo,new String[]{"id=?"}, new Object[]{id}, ConfigVo.class);
	}
}